package org.example.springredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @GetMapping("/test01")
    public String test01(){
        redisTemplate.opsForValue().set("k1","11");
        redisTemplate.opsForValue().set("k2","22");
        String k1 = redisTemplate.opsForValue().get("k1");
        System.out.println("k1: "+k1);
        return "ok";
    }

    @GetMapping("/test02")
    public String test02(){
        redisTemplate.execute((RedisConnection connection)->{
            //这里需要一个返回值,这里的返回值就是execute的返回值
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForList().leftPush("k1","11");
        redisTemplate.opsForList().leftPush("k1","22");
        redisTemplate.opsForList().leftPush("k1","33");
        String k1 = redisTemplate.opsForList().rightPop("k1");
        System.out.println("k1: "+k1);
        String k11 = redisTemplate.opsForList().rightPop("k1");
        System.out.println("k11: "+k11);
        String k12 = redisTemplate.opsForList().rightPop("k1");
        System.out.println("k12: "+k12);
        return "OK";
    }

    @GetMapping("/testSet")
    public String test03(){
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForSet().add("k1","11","22","33");
        Set<String> k1 = redisTemplate.opsForSet().members("k1");
        System.out.println(k1);
        Boolean k11 = redisTemplate.opsForSet().isMember("k1", "11");
        System.out.println("k11 : "+k11);
        Boolean k12 = redisTemplate.opsForSet().isMember("k1", "12");
        System.out.println("k12: "+k12);

        Long k13 = redisTemplate.opsForSet().size("k1");
        System.out.println("k13: "+k13);

        Long k14 = redisTemplate.opsForSet().remove("k1", "11", "22");
        System.out.println("k14: "+k14);

        return "OK2";
    }

    @GetMapping("/testZset")
    public String testZset(){
        redisTemplate.execute((RedisConnection connection)->{
           connection.flushAll();
           return null;
        });
        redisTemplate.opsForZSet().add("k1","11",11);
        redisTemplate.opsForZSet().add("k1","22",22);
        redisTemplate.opsForZSet().add("k1","33",33);

        Set<String> k1 = redisTemplate.opsForZSet().range("k1", 0, -1);
        System.out.println("k1: "+k1);

        Set<ZSetOperations.TypedTuple<String>> k11 = redisTemplate.opsForZSet().rangeWithScores("k1", 0, -1);
        System.out.println("k11: "+k11);

        Double k12 = redisTemplate.opsForZSet().score("k1", "11");
        System.out.println("k12: "+k12);

        Long k13 = redisTemplate.opsForZSet().size("k1");
        System.out.println("k13: "+k13);

        Long k14 = redisTemplate.opsForZSet().remove("k1", "11");
        System.out.println("k14: "+k14);

        Long k15 = redisTemplate.opsForZSet().size("k1");
        System.out.println("k15: "+k15);
        Long k16 = redisTemplate.opsForZSet().rank("k1", "22");
        System.out.println("k16: "+k16);
        return "OK3";
    }
    @GetMapping("/testHash")
    public String testHash(){
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForHash().put("k1","aa","11");
        redisTemplate.opsForHash().put("k1","bb","22");
        redisTemplate.opsForHash().put("k1","cc","33");
        Object o = redisTemplate.opsForHash().get("k1", "aa");
        System.out.println("o: "+o);

        Boolean b = redisTemplate.opsForHash().hasKey("k1", "aa");
        System.out.println("b: "+b);

        Long delete = redisTemplate.opsForHash().delete("k1", "aa");
        System.out.println("delete: "+delete);

        Long k1 = redisTemplate.opsForHash().size("k1");
        System.out.println("k1: "+k1);
        return "0K5";
    }
}
